var categroys = [];
var titles = [];
var hits = [];
var btn = 0;
layui.use(['jquery','layer','table','form','upload'],function(){
    var $=layui.jquery;
    var layer=layui.layer;
    getHits();

    function getHits() {
        $.ajax({
            url: '/JsStatistical/getSearchKeyWords',
            dataType: 'json',
            type: 'post',
            success: function (data) {
                var keywords = data.data;
                setVal(keywords);
            }
        });
    }
    function setVal(data){
        categroys = [];
        titles = [];
        hits = [];
        var count;
        if(data.length > 5){
            count = 5;
        }else {
            count = data.length;
        }
        $('#product').text('');
        $('#product').append('<td>' + '搜索关键字' + '</td>');
        $('#hits').text('');
        $('#hits').append('<td>' + '搜索次数' + '</td>');
        for (var i = 0; i < count; i++) {
            $('#product').append('<td>' + data[i].searchKeywords + '</td>');
            $('#hits').append('<td>' + data[i].countNum + '</td>');
            titles[i] = data[i].searchKeywords;
            hits[i] = data[i].countNum;
            var cate = {value:hits[i],name:titles[i]}
            categroys[i] = cate;
            circular();
        }
    }
    //刷新
    $('#refresh').click(function () {
        location.reload();
    })
    //返回
    $('#goBack').click(function () {
        parent.tabParentChange(3);
    })
    $(document).click(function () {
        parent.$('.rightMenu').hide();
    });
    //统计图刷新
    reload = function reload(){
        getHits()
    }
})

var option;
//扇形统计图
function circular(){
    var chartDom = document.getElementById('main');
    var myChart = echarts.init(chartDom);
    option = {
        series: [
            {
                type: 'pie',
                data: categroys,
                roseType: 'area'
            }
        ],
        xAxis: {
            show: false
        },
        yAxis: {
            show: false
        }
    };
    option && myChart.setOption(option);
}
//折线统计图
function strip() {
    var chartDom = document.getElementById('main');
    var myChart = echarts.init(chartDom);
    //var option;
    option = {
        xAxis: {
            show: true,
            type: 'category',
            data: titles
        },
        yAxis: {
            show: true,
            type: 'value'
        },
        series: [
            {
                data: hits,
                type: 'line'
            }
        ]
    };
    option && myChart.setOption(option);
}
//柱状统计图
function column(){
    var chartDom = document.getElementById('main');
    var myChart = echarts.init(chartDom);
    //var option;
    option = {
        xAxis: {
            show: true,
            data: titles
        },
        yAxis: {show: true},
        series: [
            {
                type: 'bar',
                data: hits
            }
        ]
    };
    option && myChart.setOption(option);
}
//下载图表
function downLoad(){
    var canvas = document.getElementsByTagName("canvas")
    if (canvas && canvas.length > 0) {
        var oA = document.createElement("a")
        oA.download = "Echarts图表" + ".png"
        oA.href = canvas[0].toDataURL("image/png")
        document.body.appendChild(oA)
        oA.click()
        oA.remove()
    }
}